<template>
  <div>
    <div v-if="action == 'one'">
      <el-form @submit.native.prevent>
        <el-row :gutter="24">
          <el-col :span="6">
            <el-form-item>
              <div style="display:flex">
                <el-input v-model="text" prefix-icon="el-icon-search" placeholder="请输入"
                          @keyup.enter.native="search()">
                </el-input>
                <el-button type="primary" @click="search()">查询
                </el-button>
              </div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>


      <!--表格内容-->
      <el-table ref="list" :data="tableData" style="width: 100%; margin-bottom: 5px;" border highlight-current-row
                :default-sort="{ prop: 'date', order: 'descending' }" @select-all="handleCheckedAllAndCheckedNone"
                @select="handleCheckedAllAndCheckedNone" :header-cell-style="{ textAlign: 'center' }"
                :cell-style="{ textAlign: 'center' }">
        <el-table-column property="ywbh" label="业务编号" fixed width="120px">
        </el-table-column>
        <el-table-column property="ywlx" label="业务类型" width="150px">
        </el-table-column>
        <el-table-column property="cph" label="车辆号牌" width="110px">
        </el-table-column>
        <el-table-column property="wtf" label="委托方">
        </el-table-column>
        <el-table-column property="shy" label="审核员" width="100px">
        </el-table-column>
        <el-table-column property="pgs" label="作业人" width="100px">
        </el-table-column>
        <el-table-column property="ywzt" label="业务状态" width="100px">
        </el-table-column>
        <el-table-column property="time" label="业务时效" width="100px">
        </el-table-column>
        <el-table-column label="操作" width="120px" fixed="right">

          <template slot-scope="scope">
            <el-button v-if="((role == '评估师'||role == '高级评估师') && scope.row.ywztCode==0)" type="text"
                       @click="two(scope.row.id,scope.row.ywzt,scope.row.bgFile,scope.row.templateId,scope.row.ywlx)">
              编辑
            </el-button>
            <el-button v-if="(role == '高级评估师'||role == '企业管理员') && scope.row.ywztCode == 1" type="text"
                       @click="three(scope.row.id,scope.row.ywzt,scope.row.bgFile,scope.row.templateId,scope.row.ywlx)">
              审核
            </el-button>
            <el-button v-if="(scope.row.ywztCode == 2 || scope.row.ywztCode == 3) || role == '高级评估师'||role == '企业管理员'"
                       type="text" @click="three(scope.row.id,scope.row.ywzt,scope.row.bgFile,scope.row.templateId,scope.row.ywlx)">
              查看
            </el-button>
          </template>

        </el-table-column>
      </el-table>
      <!--分页-->
      <el-pagination :page-sizes="pageSizes" :page-size="pageSize" :current-page="pageNum" :total="total"
                     @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
    <keep-alive>
      <Business v-if="action == '编辑'" :ywzt="ywzt" :ywid="ywid" :bgFile="bgFile" :templateId="templateId" :ywlx="ywlx"
                @getList="getList"></Business>
      <verify v-if="action == '审核'" :ywzt="ywzt" :ywid="ywid" :bgFile="bgFile" :templateId="templateId" :ywlx="ywlx"
              @getList="getList"></verify>
    </keep-alive>

  </div>
</template>

<script>
import {selectAll, selectsearch, create} from '@/api/evaluation/newBusiness';
import {correlationId} from '@/api/systemManage/enterpriseMaage';
import Business from './business/business.vue';
import Verify from '../verify/verify.vue';

export default {
  name: "businessList",
  components: {Business, Verify},
  data() {
    return {
      role: localStorage.getItem('role'),
      text: '',
      formFileds: {},
      tableData: [],
      action: 'one',
      // id: '',
      ywid: '',
      ywzt: '',
      bgFile: 0,
      pageNum: 1,
      pageSize: 10,
      total: 0,
      pageSizes: [10],
      input: '',

      templateId: 0,
      ywlx: ''
    }
  },
  methods: {
    two(ywid, zt, bgFile, templateId, ywlx) {
      this.action = '编辑'
      this.ywid = ywid
      this.ywzt = zt
      this.bgFile = bgFile
      this.templateId = templateId
      this.ywlx = ywlx
    },
    three(ywid, zt, bgFile, templateId, ywlx) {
      this.action = '审核'
      this.ywid = ywid
      this.ywzt = zt
      this.bgFile = bgFile
      this.templateId = templateId
      this.ywlx = ywlx
    },
    handleCheckedAllAndCheckedNone(selection) {

      // 当前选中仅一行时操作-（当前表格行高亮）
      1 != selection.length && this.$refs.list.setCurrentRow();
    },
    search() {
      this.input = this.text
      selectsearch('qyid=' + localStorage.getItem('companyId') + '&input=' + this.input + '&pageNum=' + this.pageNum + '&pageSize=' + this.pageSize).then((res) => {
        this.tableData = res.data.list
        this.total = res.data.total
        this.pageSizes = []
        let all = Math.ceil(this.total / 10)
        for (let i = 0; i < all; i++) {

          this.pageSizes.push(10 * (i + 1))
        }
        if (this.tableData) {
          var time
          for (var i = 0; i < this.tableData.length; i++) {
            time = this.getdate(this.tableData[i].shtgsj, this.tableData[i].startTime)
            if (time < 0) {
              this.tableData[i].time = '未开始'
            } else {
              this.tableData[i].time = '已作业 ' + time + ' 天'
            }
          }
        }
      })
    },
    handleSizeChange(e) {
      this.pageSize = e
      if (this.input) {
        this.search()
      } else {
        this.getList()
      }

    },
    handleCurrentChange(e) {
      this.pageNum = e
      if (this.input) {
        this.search()
      } else {
        this.getList()
      }
    },
    getList() {
      this.action = 'one'

      // this.id=1
      let id = Number(localStorage.getItem('companyId'))
      selectAll('companyId=' + id + '&pageNum=' + this.pageNum + '&pageSize=' + this.pageSize).then((res) => {
        this.tableData = res.data.list
        this.total = res.data.total
        this.pageSizes = []
        let all = Math.ceil(this.total / 10)
        for (let i = 0; i < all; i++) {

          this.pageSizes.push(10 * (i + 1))
        }
        if (this.tableData) {
          var time
          for (var i = 0; i < this.tableData.length; i++) {
            time = this.getdate(this.tableData[i].shtgsj, this.tableData[i].startTime)
            if (time < 0) {
              this.tableData[i].time = '未开始'
            } else {
              this.tableData[i].time = '已作业 ' + time + ' 天'
            }
          }
        }
      })
    },
    getdate(shtgsj, startTime) {
      var start = (new Date(startTime)).getTime()
      var end
      if (shtgsj) {
        end = (new Date(shtgsj)).getTime()
      } else {
        end = (new Date()).getTime()
      }
      var ywdate = parseInt((parseInt(end) - parseInt(start)) / (24 * 3600 * 1000))
      return ywdate
    }
  },
  mounted() {
    correlationId('userId=' + localStorage.getItem('userId')).then((res) => {
      this.id = res.data.companyId
      this.getList()
    })
  },
  activated() {
    this.getList()
  }
}
</script>

<style scoped lang="less">
.el-form {
  padding: 0 10px;
}

.el-date-editor {
  width: 100% !important;
}
</style>
